package com.qianfeng.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.dto.Page;
import com.qianfeng.entity.Department;
import com.qianfeng.mapper.DepartmentMapper;
import com.qianfeng.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Transactional
    @Override
    public void addDepartment(Department department) {
        departmentMapper.addDepartment(department);
    }

    @Transactional
    @Override
    public void deleteDepartment(Integer deptId) {
        departmentMapper.deleteDepartment(deptId);
    }

    @Transactional
    @Override
    public void updateDepartment(Department department) {
        departmentMapper.updateDepartment(department);
    }

    @Transactional(readOnly = true)
    @Override
    public List<Department> getDepartmentList() {
        return departmentMapper.getDepartmentList();
    }

    @Transactional(readOnly = true)
    @Override
    public Page getDeptPage(Integer pageNo, Integer pageSize) {
        Page page = new Page();
        PageHelper.startPage(pageNo,pageSize);
        List<Department> departmentList = departmentMapper.getDepartmentList();
        PageInfo<Department> info = new PageInfo<>(departmentList);
        page.setPageSize(info.getPageSize());
        page.setPageNo(info.getPageNum());
        page.setPageCount(info.getPages());
        page.setHasPre(info.isHasPreviousPage());
        page.setHasNext(info.isHasNextPage());
        page.setPage(info.getList());
        return page;
    }

    @Transactional(readOnly = true)
    @Override
    public Department getDepartmentByDeptId(Integer deptId) {
        return departmentMapper.getDepartmentByDeptId(deptId);
    }
}
